#include "head.h"

int net_cd(int net_fd, char *path, request_t *request) {
    
    if(!(path == NULL || path[0] == '\0')){
        strcpy(request->args, path);
    }

    if(request->args == NULL || request->args[0] == '\0'){
        printf("cd没有参数\n");
        memset(request->path, 0, sizeof(request->path));
        strcpy(request->path, "/");
        printf("cd: 当前路径为: %s\n",request->path); 
        return 0;
    }
    // 发送请求报文
    int send_size = send(net_fd, request, sizeof(request_t), 0);
    //printf("cd: args: %s\n", request->args);
    response_t res;
    memset(&res, 0, sizeof(res));
    recv(net_fd, &res, sizeof(res), 0);
    if (res.status == 1) {
        // 异常返回
        printf("cd: %s: 没有那个文件\n", res.args);
        return -1;
    }
    //printf("---响应报文---\n");
    //printf("status: %d\n", res.status);
    //printf("args: %s\n", res.args);
    //printf("-------------\n");



    if(strcmp(request->args, "..") == 0){
        memset(request->path, 0, sizeof(request->path));
        strcpy(request->path, res.args);
        printf("cd: 当前路径为: %s\n",request->path);
        return 0;
    }

    if(strcmp(request->path, "/") == 0){
        strcat(request->path, request->args);
    }else{
        strcat(request->path, "/");
        strcat(request->path, request->args);
    }
    printf("cd: 当前路径为: %s\n",request->path);

    return 0;
}
